//
//  VCForPushAVPlayer.m
//  Movie
//
//  Created by dllo on 16/3/26.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "VCForPushAVPlayer.h"
#import "MovieModel.h"
#import "NetworkHandeler.h"
#import "CellForFirstMV.h"
#import "CellForFirstDesc.h"
#import "VCForMVPlayerOfFirst.h"

#import "AppDelegate.h"
#import "WeiboSDK.h"
#define kAppKey @"3757493969"
#define kRedirectURI @"https://api.weibo.com/oauth2/default.html"
#import "WeiboSDK+Statistics.h"
#import "WBHttpRequest.h"

@interface VCForPushAVPlayer ()<UITableViewDelegate, UITableViewDataSource, NetworkHandelerDelegate,UIScrollViewDelegate>
{
    WBSDKRelationshipButton *relationshipButton;
    WBSDKCommentButton *commentButton;
}

@property (nonatomic, strong)UITableView *tableV;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)MovieModel *model;
@property (nonatomic, strong)NSIndexPath *indexNum;



@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *shareButton;



@end

@implementation VCForPushAVPlayer



- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];

}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:40 / 255.f green:40 / 255.f blue:40 / 255.f alpha:1];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(fenxiang)];
    
    
    [self createTB];
    [self handelData];
}

- (void)fenxiang{
    
    AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = kRedirectURI;
    authRequest.scope = @"all";
    
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:myDelegate.wbtoken];
    request.userInfo = @{@"ShareMessageFrom": @"ViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];
    
}

- (WBMessageObject *)messageToShare
{
    WBMessageObject *message = [WBMessageObject message];
    
    message.text = self.model.mp4;
    
    return message;
}


- (void)createTB{
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableV];
    
    [self.tableV registerClass:[CellForFirstMV class] forCellReuseIdentifier:@"Cell"];
    [self.tableV registerClass:[CellForFirstDesc class] forCellReuseIdentifier:@"Cell2"];
    
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    self.tableV.showsVerticalScrollIndicator = NO;
    self.tableV.backgroundColor = [UIColor colorWithRed:40 / 255.f green:40 / 255.f blue:40 / 255.f alpha:1];
}


- (void)handelData{
    
    NSString *str = [NSString stringWithFormat:@"http://service.vmovier.com/api/post/view?postid=%@",self.idForPush];
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];
}


- (void)handelerDidComplete:(id)result{
    self.array = [NSMutableArray array];
    
    NSDictionary *dic = [result objectForKey:@"data"];
    
    self.model = [[MovieModel alloc] init];
    
    self.model.title = [dic objectForKey:@"title"];
    
    self.model.content = [dic objectForKey:@"content"];
    self.model.coverForDetail = [dic objectForKey:@"image"];
    self.model.duration = [[dic objectForKey:@"duration"] doubleValue];
    
    NSDictionary *dicData = [dic objectForKey:@"play_link"];
    NSArray *arr = [dicData objectForKey:@"mp4"];
    self.model.mp4 = arr[0];
    
    [self.array addObject:self.model];
    
    [self.tableV reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        CellForFirstMV *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        
        MovieModel *model = [self.array objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell passModel:model];
        return cell;
    }else {
        
        CellForFirstDesc *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell2"];
        
        MovieModel *model = [self.array objectAtIndex:indexPath.row - 1];
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell2 passModel:model];
        return cell2;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        return self.view.frame.size.width / 2 + 30;
    }else{
        
        return [CellForFirstDesc heightForDesc:self.array[indexPath.row - 1]] + 45 + 20;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        self.model = self.array[indexPath.row];
        self.indexNum = indexPath;
        
        VCForMVPlayerOfFirst *play = [[VCForMVPlayerOfFirst alloc] init];
        play.urlForPlay = self.model.mp4;
        
        NSInteger time = self.model.duration;
        NSString *timeString = [NSString stringWithFormat:@"%02ld:%02ld",time/60,time% 60];
        play.playAllTime = timeString;
        
        play.receiveModel = self.model;
        [self showDetailViewController:play sender:nil];
    }
    
}

//
//- (void)showImageAtIndexPath:(NSIndexPath *)indexPath{
//
//    self.arr = self.array[indexPath.row];
//    self.indexPathNum = indexPath;
//
////    CellForFirstPushMV *cell = [self.tableV cellForRowAtIndexPath:indexPath];
//
//
//}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
